<template>
  <div>
    <el-dialog
      v-bind="$attrs"
      title="新增员工"
      :visible="showDialog"
      :close-on-click-modal="false"
      v-on="$listeners"
      @open="onOpen"
      @close="onClose"
    >
      <el-form
        ref="addEmployeeRef"
        :model="formData"
        :rules="rules"
        size="medium"
        label-width="120px"
        label-position="left"
      >
        <el-form-item label="姓名" prop="username">
          <el-input
            v-model="formData.username"
            placeholder="请输入姓名"
            clearable
            :style="{ width: '100%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input
            v-model="formData.mobile"
            placeholder="请输入手机号"
            clearable
            :style="{ width: '100%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            v-model="formData.timeOfEntry"
            form-data="yyyy-MM-dd"
            value-form-data="yyyy-MM-dd"
            :style="{ width: '100%' }"
            placeholder="请选择入职时间"
            clearable
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select
            v-model="formData.formOfEmployment"
            placeholder="请选择聘用形式"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="item in EmployeeEnum"
              :key="item.id"
              :label="item.value"
              :value="item.id"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input
            v-model="formData.workNumber"
            placeholder="请输入工号"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <el-input
            v-model="formData.departmentName"
            placeholder="请输入部门"
            clearable
            :style="{ width: '100%' }"
            @focus="getDepartments"
          ></el-input>
          <el-tree
            v-if="showTree"
            v-loading="loading"
            :data="treeData"
            default-expand-all
            :props="{ label: 'name' }"
            @node-click="onNodeClick"
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker
            v-model="formData.correctionTime"
            form-data="yyyy-MM-dd"
            value-form-data="yyyy-MM-dd"
            :style="{ width: '100%' }"
            placeholder="请选择转正时间"
            clearable
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import EmployeeEnum from '@/api/constant/employees.js'
import { addEmployee } from '@/api/employees'
export default {
  components: {},
  inheritAttrs: false,
  props: {
    showDialog: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      EmployeeEnum: EmployeeEnum.hireType,
      formData: {
        username: undefined,
        mobile: undefined,
        timeOfEntry: null,
        formOfEmployment: undefined,
        workNumber: undefined,
        departmentName: undefined,
        correctionTime: null
      },
      rules: {
        username: [{
          required: true,
          message: '请输入姓名',
          trigger: 'blur'
        }, {
          min: 1,
          max: 4,
          message: '请输入1到4个字符',
          trigger: 'blur'
        }],
        mobile: [{
          required: true,
          message: '请输入手机号',
          trigger: 'blur'
        }, {
          pattern: /^1[3-9]\d{9}$/,
          message: '手机号格式不正确',
          trigger: 'blur'
        }],
        timeOfEntry: [{
          required: true,
          message: '请选择入职时间',
          trigger: 'change'
        }],
        formOfEmployment: [{
          required: true,
          message: '请选择聘用形式',
          trigger: 'change'
        }],
        workNumber: [{
          required: true,
          message: '请输入工号',
          trigger: 'blur'
        }],
        departmentName: [{
          required: true,
          message: '请输入部门',
          trigger: 'change'
        }],
        correctionTime: []
      },
      treeData: [], // 接收树形结构
      showTree: false, // 控制树形的显示或者隐藏
      loading: false // 控制树的显示或者隐藏进度条
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onNodeClick(item) {
      this.formData.departmentName = item.name
      this.showTree = false
    },
    async getDepartments() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      this.treeData = tranListToTreeData(depts)
      this.loading = false
    },
    onOpen() {},
    onClose() {
      this.close()
    },
    close() {
      // 清空列表
      this.formData = this.$options.data().formData
      // 表单重置
      this.$refs.addEmployeeRef.resetFields()
      // 关闭弹层
      this.$emit('update:showDialog', false)
    },
    handelConfirm() {
      this.$refs['addEmployeeRef'].validate(async valid => {
        if (!valid) return
        // 发请求
        await addEmployee(this.formData)
        // 提示成功
        this.$message.success('操作成功')
        // 刷新列表
        // this.$parent 可以的到父组件的实例
        this.$parent.getEmployeeList && this.$parent.getEmployeeList()
        // 关闭弹层
        this.close()
      })
    }
  }
}
</script>
<style>
</style>
